import { createSlice } from "@reduxjs/toolkit";

const counterSlice = createSlice({
    name: 'counter',
    // 初始化 store
    initialState: {
        count: 0
    },
    // 修改数据的方法 同步方法 支持直接修改
    reducers: {
        increment: (state) => {
            state.count += 1
        },
        decrement: (state) => {
            state.count -= 1
        },
        addToNum: (state, action) => {
            state.count = action.payload
        }
    },

})

//结构出 actionCreater 函数
const { increment, decrement, addToNum } = counterSlice.actions
//获取 reducer
const counterReducer = counterSlice.reducer

//以按需导出的方式导出actionCreater
export { increment, decrement, addToNum }
//导出 reducer
export default counterReducer